package xyz.chenyongpeng.userconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.orderconsumerdubbo.service.ResourceCategoryService;
import xyz.chenyongpeng.orderconsumerdubbo.vo.ResourceCategoryVo;
import xyz.chenyongpeng.service.IZsResourceCategoryService;
import xyz.chenyongpeng.to.IndexYyTo;
import xyz.chenyongpeng.to.MapSiteTo;
import xyz.chenyongpeng.userconsumerdubbo.service.IndexLinkService;
import xyz.chenyongpeng.userconsumerdubbo.service.SystemService;
import xyz.chenyongpeng.userconsumerdubbo.service.UserService;
import xyz.chenyongpeng.userconsumerdubbo.vo.YYInfo;
import xyz.chenyongpeng.utils.R;

import java.util.*;

@RestController
@RequestMapping("/Index")
public class IndexController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private IndexLinkService indexLinkService;

    @Autowired
    private UserService userService;

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 首页默认资源
     * @param level
     * @param wx_openid
     * @return
     */
    @PostMapping("index")
    public R index(@RequestParam(name = "level",required = false) String level,
                   @RequestParam(name = "wx_openid",required = false) String wx_openid){

        Map<String,Object> parms = new HashMap<>();
        parms.put("is_delete", 0);
        if (!StringUtils.isEmpty(level)){
            parms.put("level", level);
        }

        YYInfo yyInfo = indexLinkService.getYyInfo();

        List<MapSiteTo> mapSiteTos = indexLinkService.getMapSites(parms);

        List<IndexYyTo> indexYyTos = new ArrayList<>();
        if (!StringUtils.isEmpty(wx_openid)){
            String u_id = userService.getUid(wx_openid);

            if (!StringUtils.isEmpty(u_id) && !StringUtils.isEmpty(yyInfo.getYy_goods_id())){
                indexYyTos = indexLinkService.getIndexYy(u_id,yyInfo.getYy_goods_id());
            }

            // 查询已经签到的 景点
            List<Integer> rids = userService.getSignIn(u_id);
            for (MapSiteTo m:mapSiteTos) {
                if (rids.contains(m.getR_id())){
                    m.setIs_have_sign_in(1);
                }else {
                    m.setIs_have_sign_in(0);
                }
            }
            if (rids.size() ==0){
                for (MapSiteTo m:mapSiteTos) {
                    m.setIs_have_sign_in(0);
                }
            }

        }

        List<ResourceCategoryVo> resourceCategoryVos = resourceCategoryService.getAll();

        Map<String,Object> result = new HashMap<>();
        result.put("res_cate_list", resourceCategoryVos);
        result.put("res_list", mapSiteTos);
        result.put("yy_info", yyInfo);
        result.put("reservation", indexYyTos);

        return R.ok(result);
    }

    /**
     * 获取客服电话
     * @return
     */
    @PostMapping("getServiceTel")
    public R getServiceTel(){
        Map<String,Object> result = new HashMap<>();
        result.put("data", systemService.getServiceTel());
        return R.ok();
    }
}
